package com.whsxt.buss.controller;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whsxt.buss.data.CustomerData;
import com.whsxt.buss.domain.BusCustomer;
import com.whsxt.buss.service.IBusCustomerService;
import com.whsxt.common.CodeMsg;
import com.whsxt.common.PageInfo;
import com.whsxt.common.Result;
import com.whsxt.common.util.ConstantUtil;

import cn.hutool.core.util.StrUtil;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 贺炯
 * @since 2019-09-27
 */
@RestController
@RequestMapping("/buss/customer")
public class BusCustomerController {
	
	@Autowired
	private IBusCustomerService iBusCustomerService;
	/**
	 * 	客户列表
	 * @param data
	 * @return
	 */
	@RequestMapping("/list")
	public Object list(CustomerData data) {
		IPage<BusCustomer> page = new Page<BusCustomer>(data.getPage(),data.getLimit());
		QueryWrapper<BusCustomer> queryWrapper = new QueryWrapper<BusCustomer>();
		queryWrapper.eq("available", ConstantUtil.AVAILABLE_TRUE);
		queryWrapper.like(StrUtil.isNotBlank(data.getCustomername()), "customername", data.getCustomername());
		queryWrapper.like(StrUtil.isNotBlank(data.getTelephone()), "telephone", data.getTelephone());
		queryWrapper.like(StrUtil.isNotBlank(data.getConnectionperson()), "connectionperson", data.getConnectionperson());
		iBusCustomerService.page(page, queryWrapper);
		PageInfo pageInfo = new PageInfo(page.getTotal(), page.getRecords());
		return new Result(pageInfo);
	}
	/**
	 * 	添加客户
	 * @param data
	 * @return
	 */
	@RequestMapping("/addBusCustomer")
	public Object addBusCustomer(BusCustomer data) {
		try {
			iBusCustomerService.save(data);
			return new Result(CodeMsg.CUSTOMER_ADD_SECCUSS);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(CodeMsg.CUSTOMER_ADD_ERROR);
		}
	}
	/**
	 * 	更新客户
	 * @param data
	 * @return
	 */
	@RequestMapping("/updateBusCustomer")
	public Object updateBusCustomer(BusCustomer data) {
		try {
			iBusCustomerService.updateById(data);
			return new Result(CodeMsg.CUSTOMER_UPDATE_SECCUSS);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(CodeMsg.CUSTOMER_UPDATE_ERROE);
		}
	}
	/**
	 * 	客户单个删除
	 * @param id
	 * @return
	 */
	@RequestMapping("/deleteOne")
	public Object deleteOne(Integer id) {
		try {
			iBusCustomerService.removeById(id);
			return new Result(CodeMsg.CUSTOMER_DELETE_SECCUSS);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(CodeMsg.CUSTOMER_DELETE_ERROE);
		}
	}
	
	/**
	 *    客户批量删除
	 * @param ids
	 * @return
	 */
	@RequestMapping("/batchDelete")
	public Object batchDelete(Integer [] ids) {
		System.out.println(ids);
		Collection<Serializable> idList = new ArrayList<Serializable>();
		for (Integer id : ids) {
			idList.add(id);
		}
		try {
			iBusCustomerService.removeByIds(idList);
			return new Result(CodeMsg.CUSTOMER_BATCH_DELETE_SECCUSS);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(CodeMsg.CUSTOMER_BATCH_DELETE_ERROE);
		}
	}
	
}

